package jdbcTest;

import java.sql.*;

/**
 * Created by Administrator on 2017/9/11.
 */
public class ConnectionTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库(对象)
        //需要三个参数(URL/用户名/密码)
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/ku", "root", "123456");
        if (conn != null) {
            System.out.println("ok");
        } else {
            System.out.println("error");
        }
        //操作数据Connection不能直接操作数据库
        Statement statement = conn.createStatement();
        //查询语句
        String sql = "select*from user ";
        ResultSet set = statement.executeQuery(sql);
        //假设数据库中有十条记录，则查询出的set里面就有十条记录，怎么一条一条取出来呢？
        //使用next（）方法将指针指向下一行记录（能确定那一条记录），这一条记录中的字段怎么取出来呢？？
        //使用set.getString()方法取字段的值

        while (set.next()) {
            System.out.println(set.getString(1) + "," + set.getString(2) + "," + set.getString(3));
            User user = new User();
            user.setId(set.getInt("id"));
            user.setUsername(set.getString("name"));
            user.setPassword(set.getString("alexa"));
        }
        set.close();
        statement.close();
        conn.close();
    }
}
